package com.dog.basics.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.sys.SysPositionAddDto;
import com.dog.basics.bean.dto.admin.sys.SysPositionEditDto;
import com.dog.basics.bean.model.sys.SysPosition;
import com.dog.basics.bean.vo.admin.sys.SysPositionPageVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.sys.SysPositionMapper;
import com.dog.basics.dao.sys.SysUserPosMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.sys.ISysPositionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 职位表 服务实现类
 * </p>
 *
 * @author guoy
 * @since 2020-03-19
 */
@Service
@AllArgsConstructor
public class SysPositionServiceImpl extends ServiceImpl<SysPositionMapper, SysPosition> implements ISysPositionService {
    private final SysPositionMapper sysPositionMapper;
    private final SysUserPosMapper sysUserPosMapper;

    @Override
    public IPage<SysPositionPageVo> getSysPositionPage(PageParam<SysPosition> page, String name) {
        // 查询数据
        IPage iPage = this.page(page, Wrappers.<SysPosition>lambdaQuery()
                .like(StrUtil.isNotBlank(name), SysPosition::getName, name)
                .orderByDesc(SysPosition::getCreateTime));

        List<SysPosition> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(sysPosition -> {
            SysPositionPageVo sysPositionPageVo = new SysPositionPageVo();
            BeanUtil.copyProperties(sysPosition, sysPositionPageVo);
            return sysPositionPageVo;
        }).collect(Collectors.toList()));
        return iPage;
    }

    @Override
    public Boolean saveSysPositionAddDto(SysPositionAddDto sysPositionAddDto) {
        SysPosition sysPosition = new SysPosition();
        BeanUtil.copyProperties(sysPositionAddDto, sysPosition);
        sysPosition.setCreateTime(DateUtil.currentSeconds());
        sysPosition.setCreateBy(SecurityUtils.getSysUser().getUserId());
        return this.save(sysPosition);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateSysPositionEditDto(SysPositionEditDto sysPositionEditDto) {
        SysPosition sysPosition = this.getById(sysPositionEditDto.getPositionId());
        BeanUtil.copyProperties(sysPositionEditDto, sysPosition);
        sysPosition.setUpdateTime(DateUtil.currentSeconds());
        sysPosition.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        return this.updateById(sysPosition);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSysPosition(Long[] ids) {
        //删除职位
        sysPositionMapper.deleteSysPosition(ids);

        //删除用户与职位关联
        sysUserPosMapper.deleteSysUserPos(ids);

    }
}
